package chainOfResponsibility;

import gameBasics.GameObject;
import object.CommandPost;
import tank.EnemyTank;

public class ETankCommandPostCollider implements Collider{

    /**
     * 敌对坦克与指挥所碰撞
     * @param o1 敌对坦克对象
     * @param o2 指挥所对象
     */
    @Override
    public boolean collide(GameObject o1, GameObject o2) {
        if(o1 instanceof EnemyTank enemyTank && o2 instanceof CommandPost commandPost){
            collideWith(enemyTank, commandPost);
        } else if (o1 instanceof CommandPost && o2 instanceof EnemyTank) {
            collide(o2, o1);
        }

        return true;
    }

    /**
     * 敌对坦克与指挥所的碰撞检测
     * @param enemyTank 敌对坦克对象
     * @param commandPost 指挥所对象
     */
    private void collideWith(EnemyTank enemyTank, CommandPost commandPost) {
        if(enemyTank.getRectangle().intersects(commandPost.getRectangle())){
            enemyTank.back();
        }
    }

}
